﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Citi.PTSConsolidation.Domain;
using Citi.PTSConsolidation.WebUi.Models;

namespace Citi.PTSConsolidation.WebUi.Controllers
{
    public class PearlAccountMapController : Controller
    {
        public ActionResult Exceptions()
        {
            var exceptions = PearlAccountMapModel.CreateModel(new Domain.PearlAccountMap().SelectAll<PearlAccountMap>().Where(x => x.LineType == string.Empty));

            return View(exceptions);
        }

        public ActionResult Index()
        {
            var domain = new PearlAccountMap().SelectAll<PearlAccountMap>().OrderBy(x => x.AccountL14Desc).ThenBy(x => x.Leaf);
            return View(PearlAccountMapModel.CreateModel(domain));
        }

        [HttpGet]
        public ActionResult Create()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Create(PearlAccountMapModel model)
        {
            if (!ModelState.IsValid) return View();

            var domain = PearlAccountMapModel.CreateDomain(model);
            domain.Save(domain);

            return RedirectToAction("Index");
        }

        public ActionResult Delete(int id)
        {
            var domain = new PearlAccountMap().SelectById<PearlAccountMap>(id);
            domain.Delete(domain);

            return RedirectToAction("Index");
        }

        [HttpGet]
        public ActionResult Edit(int id)
        {
            var domain = new PearlAccountMap().SelectById<PearlAccountMap>(id);
            var model = PearlAccountMapModel.CreateModel(domain);
            return View(model);
        }

        [HttpPost]
        public ActionResult Edit(PearlAccountMapModel model)
        {
            if (!ModelState.IsValid) return View();

            var domain = new PearlAccountMap().SelectById<PearlAccountMap>(model.Id);

            domain.AccountL14Desc = model.AccountL14Desc;
            domain.AditionalRule = model.AditionalRule;
            domain.Leaf = model.Leaf;
            domain.LeafDescr = model.LeafDescr;
            domain.LineType = model.LineType;

            domain.Save(domain);

            return RedirectToAction("Index");
        }
    }
}
